package chapter2.chapter2homework;

import java.util.ArrayList;

/**
 * @Project ：learn_java
 * @File ：question1.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/2/27 16:04
 * @Description ：
 * 	案例：
    	1、用一个String[]数组存点数
    	2、用一个String[]数组存花色
    	3、用一个String[]数组存大王、小王
    	4、用上面的数组，生成一副扑克牌
    	5、遍历显示全副扑克牌
    	6、模拟给4个人随机发牌，每个人11张牌
    	7、显示每个人的牌和剩余的牌
 */

public class question1 {

    // mars给的洗牌算法
    public static void shuffleOfMars(String[] poker) {
        for (int i = 0; i < poker.length; i++) {
            int index = (int) (Math.random() * poker.length);
            String temp = poker[i];
            poker[i] = poker[index];
            poker[index] = temp;
        }
    }

    public static int randomOfRange(int min, int max) {
        return (int) (Math.random() * (max - min + 1) + min);
    }

    public static ArrayList<Integer> shuffle(String[] poker) {
        ArrayList<Integer> selected = new ArrayList<Integer>();
        for (int i = 0; i < poker.length; i++) {
            int idx = randomOfRange(0, poker.length - 1);
            while (selected.contains(idx)) {
                idx = randomOfRange(0, poker.length - 1);
            }
            selected.add(idx);
        }
        return selected;
    }

    public static void main(String[] args) {
        String[] faces = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        // String[] suits = {"♠", "♡", "♢", "♣"};
        // String[] suits = {"spade", "heart", "club", "diamond"};
        // String[] jokers = {"big joker", "small joker"};
        String[] suits = {"黑桃", "红桃", "梅花", "方块"};
        // 这里添加空格是为了格式化对齐
        String[] jokers = {"大王 ", "小王 "};
        String[] poker = new String[54];

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < faces.length; j++) {
                poker[i * 13 + j] = suits[i] + faces[j];
            }
        }
        poker[52] = jokers[0];
        poker[53] = jokers[1];

        // 遍历
        System.out.println("所有的牌为：");
        for (int i = 0; i < poker.length; i++) {
            System.out.print(poker[i] + "\t");
            if (i % 10 == 9) {
                System.out.println();
            }
        }

        System.out.println();

        ArrayList<Integer> selected = shuffle(poker);
        // 随机发牌
        for (int i = 0; i < 4; i++) {
            System.out.print("第" + (i + 1) + "个人的牌为：");
            for (int j = 0; j < 10; j++) {
                System.out.print(poker[selected.get(i * 10 + j)] + "\t");
            }
            System.out.println();
        }

        System.out.println("剩下的牌为：");
        for (int i = 40; i < poker.length; i++) {
            System.out.print(poker[selected.get(i)] + "\t");
        }
    }
}
